package com.lb.jwitter.action;


import java.util.ArrayList; 
import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblThongTinHocVienSearchAction{
    private int pageSize; 
    public void setPageSize(int pageSize) {
       this.pageSize = pageSize;
     }

    public int getPageSize() {
       return pageSize;
 }

    private int page; 
    public void setPage(int page) { this.page = page;   }

    public int getPage() {
       return page;
   }

    private int pages; 
    public void setPages(int pages) { this.pages = pages;   }

    public int getPages() {
       return pages;
     }

    private String dataString; 
    public void setDataString(String dataString) { this.dataString = dataString;    }

    public String getDataString() {
       return dataString;
    }

    private Message msg; 
    public Message getMsg() { return msg; }

    private String mSSV; 
    public void setMSSV(String mSSV) {
       this.mSSV = mSSV;
   }

    public String getMSSV() {
       return mSSV;
   }

    private String hoTen; 
    public void setHoTen(String hoTen) {
       this.hoTen = hoTen;
   }

    public String getHoTen() {
       return hoTen;
   }
    
    private String lopHoc; 
    public void setLopHoc(String lopHoc) {
       this.lopHoc = lopHoc;
   }

    public String getLopHoc() {
       return hoTen;
   }

    private int chuyenNganhId; 
    public void setChuyenNganhId(int chuyenNganhId) {
       this.chuyenNganhId = chuyenNganhId;
  }

    public int getChuyenNganhId() {
       return chuyenNganhId;
  }

    private int tukhoaHoc; 
    public void setTukhoaHoc(int tukhoaHoc) {
       this.tukhoaHoc = tukhoaHoc;
  }

    public int getTukhoaHoc() {
       return tukhoaHoc;
  }

    private int denkhoaHoc; 
    public void setDenkhoaHoc(int denkhoaHoc) {
       this.denkhoaHoc = denkhoaHoc;
  }

    public int getDenkhoaHoc() {
       return denkhoaHoc;
  }

    private int khoaId; 
    public void setKhoaId(int khoaId) {
       this.khoaId = khoaId;
  }

    public int getKhoaId() {
       return khoaId;
  }

    private int heDaoTaoId; 
    public void setHeDaoTaoId(int heDaoTaoId) {
       this.heDaoTaoId = heDaoTaoId;
  }

    public int getHeDaoTaoId() {
       return heDaoTaoId;
  }

    private List<TblThongTinHocVien> tblThongTinHocViens; 
    public List<TblThongTinHocVien>  getTblThongTinHocViens() {
        return tblThongTinHocViens; 
      } 

    private TblThongTinHocVienService tblThongTinHocVienService; 

	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_ThongTin_HocVien", "xem")){ 
        return "noprivilege"; 
    }        tblThongTinHocVienService = (TblThongTinHocVienService) context.getBean("tbl_ThongTin_HocVienClient"); 
        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
    if(mSSV != null && !mSSV.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("MSSV");
        con.setValue1("%" + mSSV + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(hoTen != null && !hoTen.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("hoTen");
        con.setValue1("%" + hoTen + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(lopHoc != null && !lopHoc.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("lopHoc");
        con.setValue1("%" + lopHoc + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if(chuyenNganhId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("chuyenNganhId.id");
        con.setValue1(new Long(chuyenNganhId));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if( tukhoaHoc != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("khoaHoc");
        con.setValue1(tukhoaHoc);
        if (denkhoaHoc != 0){ 
             con.setValue2(denkhoaHoc); 
            con.setOperator("between"); 
        } 
        else{ 
            con.setOperator("ge"); 
        } 
        cons.add(con);	
    } 
   else if (denkhoaHoc != 0){ 
         ConditionReport con = new ConditionReport(); 
        con.setCol("khoaHoc");
        con.setValue1(denkhoaHoc);
        con.setOperator("le");
        cons.add(con);	
    } 
    if(khoaId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("khoaId.id");
        con.setValue1(new Long(khoaId));
        con.setOperator("eq");
        cons.add(con);	
    } 
    if(heDaoTaoId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("heDaoTaoId.id");
        con.setValue1(new Long(heDaoTaoId));
        con.setOperator("eq");
        cons.add(con);	
    } 
        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
           tblThongTinHocViens = tblThongTinHocVienService.findtblThongTinHocVienByCondition(cons,pageSize,page); 
           pages = tblThongTinHocVienService.counttblThongTinHocVienByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = ""; 
      dataString += "&mSSV=" + mSSV; 
      dataString += "&hoTen=" + hoTen; 
      dataString += "&lopHoc=" + lopHoc; 
      dataString += "&chuyenNganhId=" + chuyenNganhId; 
      dataString += "&tukhoaHoc=" + tukhoaHoc; 
      dataString += "&denkhoaHoc=" + denkhoaHoc; 
      dataString += "&khoaId=" + khoaId; 
      dataString += "&heDaoTaoId=" + heDaoTaoId; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_ThongTin_HocVien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
